﻿FlyTalk.views.SearchSearchView = Ext.extend(Ext.form.FormPanel, {
	title: 'Search',
	scroll: true,
	bodyStyle: 'background: #2f2f2e',
	
    initComponent: function () {    	
		this.backButton = new Ext.Button({
			iconCls: 'home',
			iconMask: true,
			handler: this.backButtonTap,
			scope: this
		});
		
		this.searchButton = new Ext.Button({
			iconCls: 'search',
			iconMask: true,
			handler: this.searchButtonTap,
			scope: this
		});
    	
		this.toolbar = new Ext.Toolbar({
				title: 'Search',
				doc: 'top',
				layout: 'hbox',
				items: [
					this.backButton,
					{xtype: 'spacer'},
					//this.searchButton,
				]
		});
		
		this.dockedItems = [this.toolbar];
		
		this.on('render', function () {
			this.items.getAt(2).hide();
			this.doComponentLayout(); 
		});
		
        FlyTalk.views.SearchSearchView.superclass.initComponent.call(this);
    },  
    
    items: [
        { // serach in
	        xtype: 'panel',
	    	bodyStyle: 'background: #2f2f2e',
	    	hidden: false,
	    	height: 140,
	    	width: 300,
	    	style: 'padding: 0px',
	    	items: [
	    	        {
	    	            xtype: 'textfield',
	    	            name: 'lookup',
	    	    		id: 'searchfield',
	    	           // label: 'Search',
	    	            required: true
	    	        },
	    	        {   
	    	        	bodyStyle: 'background: #2f2f2e',
	    	        	id: 'searchText',
	    	        	html:'<div style="font-size: 14px; color: white; padding: 0px; margin: 0px"></div>'
	    	        },
	    	  ]
        },
        {
        	xtype: 'button',
        	style: 'margin-left: 15px',
        	text: 'Search',
			ui: 'action',
			id: 'searchButton',
        	hidden: false,
        	width: 265,
        	centered: true,
        	handler: function(){
//        		 FlyTalk.views.TalkView.tweetToolbar.setTitle('Result');
//        		 Ext.dispatch({
// 		            controller: FlyTalk.controllers.dbController,
// 		            action: 'getSearchPost',
// 		           // keyword: word,
//        		 });
        		// alert("in search button");
        		 var word = Ext.getCmp('searchfield').getValue();
        		// alert("word is "+word);
        		 if(word == "italian restaurant" || word == "Italian Restaurant" || word == "Italian restaurant" || 
        				 "italian restaurants" || word == "Italian Restaurants" || word == "Italian restaurants" || 
        				 word == "Italian" || word == "italian" || word == "restaurant" || word == "Restaurant" ||
        				 word == "restaurants" || word == "Restaurants" ){
             		 FlyTalk.views.TalkView.tweetToolbar.setTitle('Result');
             		FlyTalk.views.SearchView.toolbar.setTitle("Search Result");
             		 Ext.getCmp('searchText').update('<div style="font-size: 14px; color: white; padding: 0px; margin: 0px"></div>');
             		 Ext.getCmp('searchText').doComponentLayout();
             		 Ext.getCmp('searchButton').setText("Search");
            		 Ext.getCmp('searchButton').doComponentLayout();
             		 FlyTalk.views.SearchSearchView.items.getAt(2).hide();
            		 Ext.dispatch({
     		            controller: FlyTalk.controllers.dbController,
     		            action: 'getSearchPost',
     		           // keyword: word,
            		 });
        		 }else{
//        			 Ext.getCmp('resultsPanel').show();
//              		 Ext.getCmp('resultsPanel').doComponentLayout();
        			 Ext.getCmp('searchText').update('<div style="font-size: small; color: white; padding: 0px; margin: 0px"><p>Sorry, no results found. </p><p style="margin-top:0.3em;">Would you like to try asking the community?</p></div>');
              		 Ext.getCmp('searchText').doComponentLayout();
              		 Ext.getCmp('searchButton').setText("Search Again");
             		 Ext.getCmp('searchButton').doComponentLayout();
        			 Ext.getCmp('askCommunity').show();
              		 Ext.getCmp('askCommunity').doComponentLayout();
        		 }
    		}
        	
        },
        {
    		xtype:'button',
    		id: 'askCommunity',
    		text: 'Ask the Community',
			ui: 'action',
    		style: 'margin: 15px',
    		width: 265,
    		hidden: true,
    		handler: function(){
    			var word = Ext.getCmp('searchfield').getValue();
    			Ext.getCmp('searchText').update('<div style="font-size: 14px; color: white; padding: 0px; margin: 0px"></div>');
        		Ext.getCmp('searchText').doComponentLayout();
        		Ext.getCmp('searchButton').setText("Search");
       		 	Ext.getCmp('searchButton').doComponentLayout();
       		 	FlyTalk.views.TalkView.tweetToolbar.setTitle('Search');
    			 
    			 Ext.dispatch({
    		            controller: FlyTalk.controllers.ftController,
    		            action: 'openQuestion',
    		            keyword: word,
    		     });
    		}
    	
        },
        {
        	xtype: 'panel',
        	id: 'resultsPanel',
        	bodyStyle: 'background: #2f2f2e',
        	hidden: true,
        	items: [
        	        {   bodyStyle: 'background: #2f2f2e',
        	        	html:'<div style="font-size: small; color: white; padding: 0px; margin: 0px">Sorry, no results found. <br><br>Would you like to try asking the community?</div>'},
        	        {
		        		xtype:'button',
		        		text: 'Ask the Community',
						ui: 'action',
		        		style: 'margin: .5em',
		        		handler: function(){
		        			var word = Ext.getCmp('searchfield').getValue();
		        			
		        			 FlyTalk.views.TalkView.tweetToolbar.setTitle('Search');
		        			 
		        			 Ext.dispatch({
		        		            controller: FlyTalk.controllers.ftController,
		        		            action: 'openQuestion',
		        		            keyword: word,
		        		     });
		        		}
		        	
        	        }
        	  ]
        }
	],
	
	backButtonTap: function () {
		FlyTalk.views.QuestionForm.items.getAt(2).reset();
		FlyTalk.views.QuestionForm.items.getAt(2).value = "";
		Ext.getCmp('searchText').update('<div style="font-size: 14px; color: white; padding: 0px; margin: 0px"></div>');
		Ext.getCmp('searchText').doComponentLayout();
		Ext.getCmp('searchButton').setText("Search");
		 	Ext.getCmp('searchButton').doComponentLayout();
		 	Ext.getCmp('askCommunity').hide();
		    Ext.getCmp('searchfield').reset();
		this.items.getAt(2).hide();
    	this.doComponentLayout(); 
        Ext.dispatch({
            controller: FlyTalk.controllers.ftController,
            action: 'openHome'
        });
    },
    
  
    searchButtonTap: function () {
    	//alert("in search");
    	//this.items.getAt(1).hide();
    	
    	//this.items.getAt(1).setVisible(true);
    	this.items.getAt(2).show();
    	this.doComponentLayout(); 
    	//this.items.getAt(1).hidden = false;
    	//this.update();
    },

});
